import {Component, OnChanges, SimpleChanges} from '@angular/core';
import {OAuthService} from "angular-oauth2-oidc";
import {Subscription} from "rxjs";
import {AuthService} from "../../service/auth.service";
import {Router, RouterLink} from "@angular/router";
import {
  MatCard,
  MatCardContent,
  MatCardHeader,
  MatCardLgImage,
  MatCardSubtitle,
  MatCardTitle, MatCardTitleGroup
} from "@angular/material/card";
import {AuthGuard} from "../../guard/auth.guard";
import {NgIf} from "@angular/common";

@Component({
  selector: 'app-base',
  standalone: true,
  imports: [
    MatCard,
    MatCardHeader,
    MatCardTitle,
    MatCardSubtitle,
    MatCardLgImage,
    MatCardContent,
    MatCardTitleGroup,
    RouterLink,
    NgIf
  ],
  templateUrl: './base.component.html',
  styleUrl: './base.component.scss',
})
export class BaseComponent implements  OnChanges {

  title = 'els-client'
  text = ''
  helloSubscription: Subscription | undefined
  imagePath = './assets/images/ves.jpg'
  constructor(private oauthService: OAuthService,
              private authService: AuthService,
              private router: Router) {
  }
  login() {
    this.router.navigate(['/cabinet']).then(r =>  true)
  }


  getData() {
    this.helloSubscription = this.authService.getUser().subscribe(response => {
      this.text= response
    })
  }

  logout() {
    this.oauthService.logOut()
  }

  // ngOnInit(): void {
  //   if(this.oauthService.hasValidIdToken())  {
  //     console.log(this.oauthService.hasValidIdToken())
  //   }
  // }

  ngOnChanges(changes: SimpleChanges): void {
    console.log('Запуск changed')
    changes.hasOwnProperty('oauthService') && changes['oauthService'].currentValue.hasValidIdToken() && changes['oauthService'].currentValue.getAccessToken()
    console.log(changes.hasOwnProperty('oauthService'))
    console.log(changes['oauthService'].currentValue.hasValidIdToken())
  }

  protected readonly AuthGuard = AuthGuard;
}
